package com.fyy.socket.socketServer;

import com.fyy.socket.model.Student;

import java.io.*;
import java.net.Socket;

/**
 * @Author fyy
 * @Description Socket服务端
 * @Date 2020/11/30 14:58
 */
public class SocketServer implements Runnable{

    private InputStream input = null;
    private OutputStream output = null;
    private ObjectOutputStream obj = null;
    private ObjectInputStream objin = null;
    private PrintWriter pw = null;
    private Socket s = null;

    SocketServer(Socket s){
        this.s = s;
    }

    @Override
    public void run() {
        try {
            input = s.getInputStream ();
            output = s.getOutputStream ();
            obj = new ObjectOutputStream (output);
            pw = new PrintWriter (output);
            //发送一个整数
            obj.writeInt (1617);
            //发送一个浮点数
            obj.writeDouble (1213.1212);
            //发送一个学生对象
            Student ss = new Student (2232, "胡歌");
            obj.writeObject (ss);
            pw.flush ();
            obj.flush ();

            objin = new ObjectInputStream (input); //读取字节流
            Student stu = (Student) objin.readObject ();//readObject要转换你设定的对象
            System.out.println ("服务端接收消息：" + stu);
        } catch (Exception e) {
            e.printStackTrace();
        }


    }
}
